package com.clgk.wechatmall.common.aop;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.clgk.wechatmall.common.Response;
import com.clgk.wechatmall.common.ThreadLocals;
import com.clgk.wechatmall.common.constants.ErrorConstants;
import com.clgk.wechatmall.manager.entity.ManagerVo;

@Aspect
@Order(1)
@Component
public class I18nAop {

	private Logger logger = LoggerFactory.getLogger(I18nAop.class);
	
	
	
	@Around("execution(* com.mykj.lamp.*.action..*(..))")  
	public Object aroundMethod(ProceedingJoinPoint pjd){  

	    Object result = null;  
	    String methodName = pjd.getSignature().getName();  
	    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

//		ServletWebRequest servletWebRequest=new ServletWebRequest(request);
//		HttpServletResponse response=servletWebRequest.getResponse();
	    try {  
	    	String lanKey = request.getLocale().toLanguageTag().toLowerCase();
	        //前置通知  
	        logger.info("The method|" + methodName + "|language|" + lanKey);  
	        //执行目标方法  
	        if(hadPower(methodName)){
	        	result = pjd.proceed();  
	        }else{
	        	Response<Object> response = new Response<Object>();
	        	ErrorConstants.setResponse(response, ErrorConstants.NO_POWER);
	        	result = response;
	        }
	        //返回通知  
//	        logger.info("The method " + methodName + " ends with " + JSON.toJSONString(result));  
	    } catch (Throwable e) {  
	        //异常通知  
	    	logger.error("The method " + methodName + " occurs exception:" + e);  
	        throw new RuntimeException(e);  
	    }  
	    //后置通知  
	    logger.info("The method " + methodName + " ends");  

	    return result; 
	}
	public static void main(String[] args) {
		Pattern p = Pattern.compile("\\$\\{[^}]+}");
        Matcher matcher = p.matcher("{\"deviceid\":\"${100}\"},{\"counts\":-1}");
        while(matcher.find()){
        	String m = matcher.group();
        	System.out.println(m);
        }
	}
	
	public boolean hadPower(String method){
		ManagerVo backUser = ThreadLocals.getBackUser();
		if(backUser == null || backUser.getType()<=9){
			return true;
		}
//		if(checkMethod(method)){
//			return true;
//		}
		String power = backUser.getPower();//增、改、删
		method = method.toLowerCase();
		if(method.startsWith("save")
				|| method.startsWith("add")
				|| method.startsWith("batchsave")
				|| method.startsWith("batchadd")){
			if(power.startsWith("1")){
				return true;
			}
			return false;
		}
		if(method.startsWith("update")){
			if(power.substring(1).startsWith("1")){
				return true;
			}
			return false;
		}
		if(method.startsWith("del")){
			if(power.substring(2).startsWith("1")){
				return true;
			}
			return false;
		}
		return true;
	}
	
	static List<String> noCheckMethod = new ArrayList<String>();
	static{
		noCheckMethod.add("adminLogin");
	}
	
	public boolean checkMethod(String method){
		for(String m : noCheckMethod){
			if(method.equals(m)){
				return true;
			}
		}
		return false;
	}
	
//	@AfterReturning(pointcut="execution(* com.mykj.smartHome.*.action..*(..))")
//	public void afterResult(JoinPoint point, Object result){
//		logger.info("result:" + JSON.toJSONString(result));
//	}
}
